package com.jiang.booksystem.controller;


import com.jiang.booksystem.core.ApiResponse;
import com.jiang.booksystem.dao.entity.User;
import com.jiang.booksystem.dao.entity.UserBookBorrow;
import com.jiang.booksystem.dao.entity.dos.UserBookBorrowAggre;
import com.jiang.booksystem.manager.UserBookBorrowManager;
import com.jiang.booksystem.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;
    @Autowired
    UserBookBorrowManager userBookBorrowManager;
//    @GetMapping("/user")
//    public ModelAndView User(){
//        ModelAndView mv=new ModelAndView("user");
//        ArrayList<User> users= userManager.search();
//        mv.addObject("users",users);
//        return mv;
//    }

    @GetMapping("/user/add")
    public ApiResponse<Void> add(
            @RequestParam(value = "name", required = true) String name,
            String sex,
            int age,
            String password) throws Exception {


        User a = new User();
        a.setName(name);
        a.setSex(sex);
        a.setAge(age);
        a.setPassword(password);

        userManager.add(a);

        return new ApiResponse<>(1, "添加成功", null);

    }

    @GetMapping("/user/search")
    public ApiResponse<List<User>> search(Integer id) {
        List<User> data = userManager.search(id);
        return new ApiResponse<>(1, null, data);
    }

    //修改密码
    @GetMapping("/user/password")
    public ApiResponse<Void> password(String name, String password) {
        userManager.password(name, password);
        return new ApiResponse<>(1, "密码修改成功", null);
    }

    //修改基本信息
    @GetMapping("/user/update")
    public ApiResponse<Void> update(String name, String sex, int age) {
        userManager.update(name, sex, age);
        return new ApiResponse<>(1, "修改成功", null);
    }

    @GetMapping("/user/delete")
    public ApiResponse<Void> delete(String name) {

        userManager.delete(name);
        return new ApiResponse<>(1, "删除成功", null);
    }

    //登录
    @GetMapping("/user/signIn")
    public ApiResponse<String> signIn(String name, String password) {
        String token = userManager.signIn(name, password);
        return new ApiResponse<String>(1, "登录成功", token);
    }

    //申请借阅书本
    @GetMapping("/user/applyBorrow")
    public ApiResponse<Void> applyBorrow(@RequestParam(value = "userId", required = true) int userId,
                                         @RequestParam(value = "bookId", required = true) int bookId) {
        userBookBorrowManager.applyBorrow(userId, bookId);
        return new ApiResponse<>(1, "提交申请借阅书本成功", null);
    }

    //查看借阅记录
    @GetMapping("user/searchApply")
    public ApiResponse<List<UserBookBorrowAggre>> searchApply(int userId) {
        List<UserBookBorrowAggre> data = userManager.searchApply(userId);
        return new ApiResponse<>(1, null, data);
    }

    //还书
    @GetMapping("user/returnBooks")
    public ApiResponse<Void> returnBooks(@RequestParam(value = "userId", required = true) int userId,
                                         @RequestParam(value = "bookId", required = true) int bookId) {

        userManager.returnBooks(userId, bookId);
        return new ApiResponse<>(1, "归还书本成功", null);
    }

}
